<?php

namespace App\Http\Models;

use App\Models\ActivityInformation;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Watson\Rememberable\Rememberable;

class Activity extends BaseModel
{
    use HasDateTimeFormatter;
    use Rememberable;
    // use SoftDeletes;

    protected $rememberCacheTag = 'henlius_activity_cache';

    protected $cache_expire_in_minutes = 1440;

    protected $table = 'activity';

    protected $fillable = [
        'id',
        'activity_name',
        'desc',
        'cover',
        'remarks',
        'created_at',
        'updated_at',
        'is_del',
        'status'
    ];

    protected $hidden = [
        'status',
        'is_del',
    ];

    public function getCoverAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }

    protected $casts = [
        'created_at' => 'datetime:Y-m-d',
        'expert_judge' => 'array'
    ];
    /**
     * 活动专题列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-20
     * @param [int] $page
     * @param [int] $limit
     * @param [array] $where
     * @param [array] $field
     * @return mixed
     */
    public function getList($page, $limit, $where = [], $field = ['*'])
    {
        $where = function ($query) use ($where) {

            $query->where('is_del', 0);

            $query->where('status', 1);

            if (isset($where['name']) && $where['name'] != '') {
                $query->where('activity_name', 'like', '%' . $where['name'] . '%');
            }

            if (isset($where['cate_id']) && $where['cate_id'] != '') {
                $query->where('cate_id', $where['cate_id']);
            }
        };

        // return $this->paginate($page, $limit, $where, $field, 'desc', 'page', '2');
        return $this->paginate($page, $limit, $where, $field, 'desc', 'page',  $this->cache_expire_in_minutes);
    }

    /**
     * 获取病例专题下拉菜单列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-01-05
     * @return void
     */
    public static function getTopicCategoryList()
    {
        return self::where('status', 1)->where('is_del', 0)->remember(1440)->select('id', 'activity_name')->orderBy('id', 'desc')->get(); //->remember(1440)
    }


    /**
     * 获取赛区信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-06
     * @param int $act_id
     * @return void
     */
    public static function getDivisionInfo($act_id)
    {
        $info = ActivityInformation::where('act_id', $act_id)->first();

        if (empty($info)) {
            return [];
        }
        $activity = Activity::find($act_id);
        $info['activity_name'] = $activity->activity_name;
        $info['activity_cover'] = $activity->cover;
        $info['remarks'] = $activity->remarks;
        return $info->toArray();
    }
}
